package com.lyf.common.api;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

/**
 * @author lyf
 */
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
@ToString
@Data
public class CommonResult<T>  {

    @ApiModelProperty(required = true, name = "状态码", value = "状态码")
    private int code;


    @ApiModelProperty(required = true, name = "返回消息", value = "返回消息", example = "success")
    private String msg;



    @JsonIgnoreProperties(ignoreUnknown = true)
    @ApiModelProperty(required = false)
    private T data;





    /**
     * 不建议业务模块使用。网关返回报文使用
     * @param code
     * @param msg
     */
    public CommonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public CommonResult() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
    }

    protected CommonResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode,String message) {
        return new CommonResult<T>(errorCode.getCode(), message, null);
    }
    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param data 返回的数据
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode,T data) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMsg(), data);
    }
    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 网关拦截、未登录返回结果
     */
    public static <T> CommonResult<T> unauthorizedNoHeader() {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED_NO_HEADER_AUTHORIZATION.getCode(),
                ResultCode.UNAUTHORIZED_NO_HEADER_AUTHORIZATION.getMsg());
    }

    /**
     * 网关拦截、invalid authorization
     */
    public static <T> CommonResult<T> unauthorizedInvalidAuthorization() {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED_INVALID_AUTHORIZATION.getCode(),
                ResultCode.UNAUTHORIZED_INVALID_AUTHORIZATION.getMsg());
    }

    /**
     * 限流
     */
    public static <T> CommonResult<T> tooManyRequests() {
        return new CommonResult<T>(ResultCode.TOO_MANY_REQUESTS.getCode(),
                ResultCode.TOO_MANY_REQUESTS.getMsg());
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), data);
    }



    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
